﻿//
// Created by Administrator on 2022/1/17.
//

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include <wx/app.h>
#include <wx/cmdline.h>
#include <wx/filename.h>
#include <mysql.h>

#include "func.h"
#include "Python.h"

#define MY_SQL_HOST "git.my.host"
#define MY_SQL_USER "gogoqiu"
#define MY_SQL_PASS "512512ZoPoqiu"
#define MY_SQL_DB "clientV1"
#define MY_SQL_PORT 3306

// ============================================================================
// implementation
// ============================================================================

static const wxCmdLineEntryDesc cmdLineDesc[] =
        {
                { wxCMD_LINE_SWITCH, "h", "help", "show this help message",
                  wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
                //{ wxCMD_LINE_SWITCH, "d", "dummy", "a dummy switch",
                 // wxCMD_LINE_VAL_NONE, 0 },
                { wxCMD_LINE_SWITCH, "s", "secret", "a secret switch",
                  wxCMD_LINE_VAL_NONE, wxCMD_LINE_HIDDEN },
                // ... your other command line options here...
                { wxCMD_LINE_SWITCH, "l", "list", "process a file list",
                  wxCMD_LINE_VAL_STRING, 0 },
                { wxCMD_LINE_SWITCH, "f", "file", "process a file",
                  wxCMD_LINE_VAL_STRING, 0 },
                { wxCMD_LINE_SWITCH, "d", "dir", "process a dir",
                  wxCMD_LINE_VAL_STRING, 0 },
                //{ wxCMD_LINE_OPTION, ("d"), ("debug"), ("specify a debug level") },
                { wxCMD_LINE_PARAM,  nullptr, nullptr, ("input file"),
                  wxCMD_LINE_VAL_STRING,
                  wxCMD_LINE_PARAM_OPTIONAL },
                wxCMD_LINE_DESC_END
        };

static int process_a_file( wxFileName fileName, MYSQL * conn_ptr ){
    RepSession repSession;
    repSession.init( fileName, nullptr, conn_ptr, true );
    repSession.postFile();
    return 0;
}

static int process_a_dir( wxFileName fileName, MYSQL * conn_ptr, bool traverse ){
    //build the list
    if( traverse ){

    }else{
        //不支持遍历，则不允许有子目录
    }
    return 0;
}

static void addLog( const wxString& log ){
    //wxPrintf("%s\n", log );
    wxString tmp;
    tmp.sprintf( "%s\n", log );
    std::cout << tmp.mbc_str();
}

static int process_a_list_file( wxString path ){
    return 0;
}

static MYSQL* init_mysql( MYSQL * conn_ptr ){
    conn_ptr = mysql_init(nullptr);
    if (!conn_ptr) {
        //0
        //printf("mysql_init failed\n");
        //fail
        addLog("mysql初始化失败");
        return conn_ptr;
    }
    conn_ptr = mysql_real_connect( conn_ptr, MY_SQL_HOST, MY_SQL_USER, MY_SQL_PASS,
                                   MY_SQL_DB, MY_SQL_PORT, nullptr, 0);
    if (!conn_ptr) {
        addLog("mysql连接失败");
        return conn_ptr;
    }else
        addLog("mysql已连接");
    return conn_ptr;
}

static wxString getInputFileName( wxCmdLineParser parser ){
    wxString cmdFilename = parser.GetParam(0);
    // 在windows系统上,如果通过资源管理器打开一个文件的时候,
    // 传递的是短格式的文件名
    // 因此我们可以把它变成长格式文件名
    wxFileName fName(cmdFilename);
    fName.Normalize(wxPATH_NORM_LONG|wxPATH_NORM_DOTS|
                    wxPATH_NORM_TILDE|wxPATH_NORM_ABSOLUTE);
    cmdFilename = fName.GetFullPath();
    return cmdFilename;
}

static wxString getListFileName( wxCmdLineParser parser ){

}

static wxCharBuffer ToGB2312(wxString const& str){
    return str.mb_str(wxCSConv(wxT("gb2312")));
}

static int getList( MYSQL* conn_ptr, wxCmdLineParser* parser ){
    return 0;
}

static int getFile( MYSQL* conn_ptr, wxCmdLineParser* parser ){
    conn_ptr = init_mysql( conn_ptr );
    if( conn_ptr == nullptr ){
        return -1;
    }
    wxPrintf("f given...\n");
    wxString cmdFilename = parser->GetParam(0);
    //bug:G:\eMule\eMule\Incoming
    //G:\eMule\Incoming
    cmdFilename.Replace("\\", "/");
    // 在windows系统上,如果通过资源管理器打开一个文件的时候,
    // 传递的是短格式的文件名
    // 因此我们可以把它变成长格式文件名
    addLog( cmdFilename );
    wxFileName fName(cmdFilename);
    fName.Normalize(wxPATH_NORM_LONG|wxPATH_NORM_DOTS|
                    wxPATH_NORM_TILDE|wxPATH_NORM_ABSOLUTE);
    cmdFilename = fName.GetFullPath();
    //wxPrintf( wxT("%s\n"), cmdFilename );
    if( fName.FileExists() ) {
        addLog("启动传输...");
        return process_a_file(fName, conn_ptr);
    }else {
        addLog("文件不存在");
        return -1;
    }
}

int main(int argc, char** argv) {
    //test code
    //65001 utf8
    //https://blog.csdn.net/sq8706/article/details/118035054
    /*
     * Locale is: en_GB
Date is: Thu 23 Aug 2012 06:39:32 MST
Locale is: de_DE
Date is: Do 23 Aug 2012 06:39:32 MST
     */
    /*
    printf("\n\rlocale is %s", setlocale(LC_ALL,nullptr));//输出一下语言环境
    //wxMessageBox(  setlocale(LC_ALL,nullptr), wxT("完整"));
    wxString s = wxT( "我们" );
    wxPrintf( "%s\n", ToGB2312( "我们" ) );
    wxPrintf( "%s\n", wxT( "我们" ) );
    wxPrintf( "%s\n", s.wc_str() );
    wxPrintf( "%s\n", wxString::FromUTF8("我们") );
    const wchar_t* a = L"欢迎使用wxWidgets!";
    wxPrintf( "%s\n", a );
    const char* aa = "你好";
    //wxMessageBox(wxString(aa), wxT("完整"));
    wxPrintf( "%s\n", aa );
    return 0;
     */
    //std::cout << "我们!\n";
    //std::cout<< "你们\n";
    //wxPrintf( wxT("%s\n"), "你们" );
    //wxString s = wxT( "我们\n" );
    //std::cout << s.mbc_str() ;
    MYSQL *conn_ptr = nullptr;

    wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");

    wxInitializer initializer;
    if (!initializer) {
        fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
        return -1;
    }

    wxCmdLineParser parser(cmdLineDesc, argc, argv);
    wxString cmdFilename;

    switch (parser.Parse())
    {
        case -1:
            // help was given, terminating
            wxPrintf("What?\n");
            break;

        case 0:
            // everything is ok; proceed
            /*
            if (parser.Found("d"))
            {
                wxPrintf("Dummy switch was given...\n");

                while (true)
                {
                    wxChar input[128];
                    wxPrintf("Try to guess the magic number (type 'quit' to escape): ");
                    if (!wxFgets(input, WXSIZEOF(input), stdin))
                        break;

                    // kill the last '\n'
                    input[wxStrlen(input) - 1] = 0;

                    if (wxStrcmp(input, "quit") == 0)
                        break;

                    long val;
                    if (!wxString(input).ToLong(&val))
                    {
                        wxPrintf("Invalid number...\n");
                        continue;
                    }

                    if (val == 42)
                        wxPrintf("You guessed!\n");
                    else
                        wxPrintf("Bad luck!\n");
                }
            }*/
            if (parser.Found("s"))
            {
                wxPrintf("s given...\n");
            }

            if (parser.Found("l"))
            {
                wxPrintf("l given...\n");
                wxString cmdFilename = parser.GetParam(0);
                // 在windows系统上,如果通过资源管理器打开一个文件的时候,
                // 传递的是短格式的文件名
                // 因此我们可以把它变成长格式文件名
                wxFileName fName(cmdFilename);
                fName.Normalize(wxPATH_NORM_LONG|wxPATH_NORM_DOTS|
                                wxPATH_NORM_TILDE|wxPATH_NORM_ABSOLUTE);
                cmdFilename = fName.GetFullPath();
                wxPrintf( cmdFilename );
            }

            if (parser.Found("f"))
            {
                getFile( conn_ptr, &parser );
            }
            break;

        default:
            break;
    }

    if (argc == 1) {
        // If there were no command-line options supplied, emit a message
        // otherwise it's not obvious that the sample ran successfully
        wxPrintf("Welcome to the wxWidgets 'console' sample!\n");
        wxPrintf("For more information, run it again with the --help option\n");
    }

    if ( parser.GetParamCount() != 0 )
    {
        //wxPrintf("Welcome to the wxWidgets 'console' sample!\n");
        getFile( conn_ptr, &parser );
    }
    // do something useful here

    return 0;
}